home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio / Patchmix / Source / PaletteView.m < prev    next >
Text File  |  1992-08-18  |  6KB  |  211 lines

  1. // PaletteView.m
  2. // By Jayson Adams, NeXT Developer Support Team
  3. // You may freely copy, distribute and reuse the code in this example.
  4. // NeXT disclaims any warranty of any kind, expressed or implied, as to its
  5. // fitness for any particular use.
  6. // Modified by Mara Helmuth to accomodate multiple images.
  7.  
  8. // Unit Generator tiffs are 80w,80h, converters 80x66(overlap height-wise).
  9. // PaletteView contains 18(+2repeated) ug tiffs and 6 converters:
  10. //  Up to release 1.1:
  11. // Oscil   Buzz    Rand   Evp    Reson       pchcps
  12. //                                   pchoct
  13. // Comb    Pluck   Delay  Reverb Ws    cpspch
  14. // +           -           *          /          Sound         cpsoct
  15. //                                   octpch
  16. // Preset  Pfield  Create                    octcps 
  17.  
  18. //   Release 1.2:
  19. // Oscil      Buzz    Rand       Evp        Reson        pchcps
  20. //                                           pchoct
  21. // Comb     Pluck             Allpass     Reverb     cpspch       delay will be in middle
  22. // +           -           *              /              WS         cpsoct
  23. //                                           octpch
  24. // Sound    Pfield      Value    C-expr    ???            octcps 
  25.  
  26. #import <math.h>
  27. #import <appkit/NXImage.h>
  28. #import <appkit/Window.h>
  29.  
  30. #import "TransparentWindow.h"
  31.  
  32. #import "PaletteView.h"
  33.  
  34.  
  35. @implementation PaletteView
  36.  
  37.  
  38. /* instance methods */
  39.  
  40. - (BOOL)acceptsFirstMouse
  41. {
  42.     return YES;
  43. }
  44.  
  45. - setImages
  46. {
  47.     int num, row, col;
  48.     
  49.   /* setup images */
  50.    image[0] = [NXImage findImageNamed:"Sound"];
  51.    image[1] = [NXImage findImageNamed:"Pfield"];
  52.    image[2] = [NXImage findImageNamed:"Preset"];       // Value
  53.    image[3] = [NXImage findImageNamed:"Cexpr"];       // C expression
  54.    image[4] = [NXImage findImageNamed:"User"];
  55.    image[5] = [NXImage findImageNamed:"Add"];
  56.    image[6] = [NXImage findImageNamed:"Sub"];
  57.    image[7] = [NXImage findImageNamed:"Mult"];
  58.    image[8] = [NXImage findImageNamed:"Div"];
  59.    image[9] = [NXImage findImageNamed:"WS"];
  60.    image[10] = [NXImage findImageNamed:"Comb"];
  61.    image[11] = [NXImage findImageNamed:"Pluck"];
  62.    image[12] = [NXImage findImageNamed:"NotWorking"];     //  Should be Allpass, allpass is delay 
  63.    image[13] = [NXImage findImageNamed:"Allpass"];
  64.    image[14] = [NXImage findImageNamed:"Reverb"];
  65.    image[15] = [NXImage findImageNamed:"Oscil"];
  66.    image[16] = [NXImage findImageNamed:"Buzz"];
  67.    image[17] = [NXImage findImageNamed:"Rand"];
  68.    image[18] = [NXImage findImageNamed:"Evp"];
  69.    image[19] = [NXImage findImageNamed:"Reson"];
  70.    image[20] = [NXImage findImageNamed:"octcps"];
  71.    image[21] = [NXImage findImageNamed:"octpch"];
  72.    image[22] = [NXImage findImageNamed:"cpspch"];
  73.    image[23] = [NXImage findImageNamed:"cpsoct"];
  74.    image[24] = [NXImage findImageNamed:"pchoct"];
  75.    image[25] = [NXImage findImageNamed:"pchcps"];
  76.    num = 0;
  77.    for(row = 0; num < PAL_IMAGES; row++) {
  78.         for(col = 0; col < COLS; col++) {
  79.  
  80.    /* the hotRect marks the image's bounds */
  81.             [image[num] getSize:&(hotRect[num].size)];
  82.  
  83.             hotRect[num].origin.x = col*ICON_SIZE;
  84.             hotRect[num].origin.y = row*ICON_SIZE;
  85.             num++;
  86.             if(num == PAL_IMAGES)
  87.                 break;
  88.     }
  89.     }
  90.  
  91. // do converter images
  92.     for(row = 0; row < CONV_IMAGES; row++) {
  93.            [image[num] getSize:&(hotRect[num].size)];
  94.  
  95.         hotRect[num].origin.x = COLS * ICON_SIZE; // right hand column
  96.         hotRect[num].origin.y = row*CONV_HEIGHT;
  97.         num++;
  98.     }    
  99.     
  100.     [self display];
  101.     
  102.     return self;
  103. }
  104.  
  105. - mouseDown:(NXEvent *)theEvent
  106. {
  107.     id        imageWindow;
  108.     NXRect    windowRect;
  109.     NXPoint    hitPoint, offset, mouseLocation;
  110.     int        chosen, midx, midy, row, col;
  111.         
  112.   /* return if not within the image's bounds */
  113.     hitPoint = theEvent->location;
  114.     [self convertPoint:&hitPoint fromView:nil];
  115.  
  116.     midx = ICON_SIZE*(COLS+1)/2;  
  117.     midy = ICON_SIZE*ROWS/2;
  118.     chosen = -1;
  119.     
  120.     if(hitPoint.x < midx) {            // find the column
  121.         if(hitPoint.x < ICON_SIZE)
  122.             col = 0;
  123.         else if(hitPoint.x < 2*ICON_SIZE)
  124.             col = 1;
  125.         else
  126.             col = 2;
  127.     }
  128.     else {
  129.         if(hitPoint.x < midx+ICON_SIZE)
  130.             col = 3;
  131.         else if(hitPoint.x < ICON_SIZE*5)
  132.             col = 4;
  133.         else
  134.             col = 5;
  135.     }
  136.     if(col == 5) {                     // converter, find row
  137.         if(hitPoint.y < midy) {
  138.             if(hitPoint.y < CONV_HEIGHT) 
  139.                 chosen = 20;
  140.             else if(hitPoint.y < CONV_HEIGHT*2)
  141.                 chosen = 21;
  142.             else
  143.                 chosen = 22;
  144.         }
  145.         else
  146.             if(hitPoint.y < midy+CONV_HEIGHT)
  147.                 chosen = 23;
  148.             else if(hitPoint.y < CONV_HEIGHT*5)
  149.                 chosen = 24;
  150.             else
  151.                 chosen = 25;
  152.     }
  153.     else {                            // not a converter ugen, find row
  154.         if(hitPoint.y < midy) {
  155.             if(hitPoint.y < ICON_SIZE)
  156.                 row = 0;
  157.             else
  158.                 row = 1;
  159.         }
  160.         else {
  161.             if(hitPoint.y < midy+ICON_SIZE)
  162.                 row = 2;
  163.             else 
  164.                 row = 3;
  165.         }
  166.         chosen = row*5+col;
  167.     }    
  168.        
  169.       
  170.   // convert the hotRect to screen-based coordinates
  171.     windowRect = hotRect[chosen];                
  172.     dragImage = image[chosen];
  173.     
  174.     [patchVw setUgen:chosen];  // tell PatchView which image
  175.     
  176.     [self convertRect:&windowRect toView:nil];
  177.     [window convertBaseToScreen:&windowRect.origin];
  178.     
  179.     imageWindow = [[TransparentWindow alloc] initForImage:dragImage
  180.                          at:&(windowRect.origin)
  181.                          forView:self];
  182.     
  183.   /* compute the offset from the image's origin to the mouse location */
  184.         offset.x = hitPoint.x - NX_X(&hotRect[chosen]);
  185.         offset.y = hitPoint.y - NX_Y(&hotRect[chosen]);
  186.     
  187.   /* convert the mousedown location to screen coordinates */
  188.     mouseLocation = theEvent->location;
  189.     [window convertBaseToScreen:&mouseLocation];
  190.     
  191.   /* go into the dragging loop */
  192.        [imageWindow dragFromMouseDown:&mouseLocation mouseOffset:&offset];
  193.     
  194.     return self;
  195. }
  196.  
  197.  
  198. - drawSelf:(NXRect *)rects :(int)count
  199. {
  200.     int i;
  201.     NXDrawGrayBezel(&bounds, NULL); /* not sure I like this */
  202.     
  203.     for(i = 0; i < PAL_IMAGES+CONV_IMAGES; i++) {
  204.         [image[i] composite:NX_SOVER toPoint:&hotRect[i].origin];
  205.     }
  206.     
  207.     return self;
  208. }
  209.  
  210. @end
  211.